const fs = require('fs')
function readfs1() {
  return new Promise((resolve,reject)=>{
    fs.readFile('./fs1.txt',(err,data)=>{
      resolve(data.toString())
    })
  })
}
function readfs2() {
  return new Promise((resolve,reject)=>{
    fs.readFile('./fs2.txt',(err,data)=>{
      resolve(data.toString())
    })
  })
}
function readfs3() {
  return new Promise((resolve,reject)=>{
    fs.readFile('./fs3.txt',(err,data)=>{
       resolve(data.toString())
    })
  })
}

function mineReadFile(path){
  return new Promise((resolve,reject)=>{
    fs.readFile(path,(err,data)=>{
      if (err){
        reject(err)
      }
      resolve(data.toString())
    })
  })
} 

//利用then函数实现连续读取文件内容
mineReadFile('./fs1.txt').
then((val)=>{
  console.log(val)
  return mineReadFile('./fs2.txt')
}).
then((val)=>{
  console.log(val)
  return mineReadFile('./fs3.txt')
}).
then((val)=>{
  console.log(val)
})
//await执行异步函数的成结果
//利用async和await实现连读读取
async function main() {
  try{
    let res1 = await readfs1()
    console.log(res1)
    let res2 = await readfs2()
    console.log(res2)
    let res3 = await readfs3()
    console.log(res3)
  }catch(e){
    console.log(e)
  }
  return;
}
let res  = main()
console.log(res)
